{% extends 'admin/_layout.html' %}

{% block body %}
<div class="row">
    <div class="col-md-12 grid-margin stretch-card">
        <div class="card">
            <div class="card-body">
                <h4 class="card-title">{{this.page_title}}</h4>
                <p class="card-description">
                    <a href="{{request.args.get('back')}}" class="btn btn-info">返回</a>
                </p>
                <form class="forms-sample" id="form-data">
                    {% for item in cfg %}
                        <div class="form-group row">
                            <label for="{{item['field']}}" class="col-sm-3 col-form-label">{{item['label']}}</label>
                            <div class="col-sm-9">
                                {% if 'text' == item['type'] %}
                                    <input type="text" class="form-control" id="{{item['field']}}" name="{{item['field']}}" placeholder="" value="{{model.get(item['field'],'')}}">
                                {% endif %}
                                {% if 'textarea' == item['type'] %}
                                    <textarea class="form-control" name="{{item['field']}}" placeholder="">{{model.get(item['field'],'')}}</textarea>
                                {% endif %}
                                {% if 'select' == item['type'] %}
                                    <select name="{{item['field']}}" class="form-control">
                                        {% for one in item['options'] %}
                                            {% if one['value'] == model.get(item['field']) %}
                                                <option value="{{one['value']}}" selected>{{one['name']}}</option>
                                            {% else %}
                                                <option value="{{one['value']}}">{{one['name']}}</option>
                                            {% endif %}
                                        {% endfor %}
                                    </select>
                                {% endif %}
                                {% if 'radio' == item['type'] %}
                                    {% for one in item['options'] %}
                                        {% if one['value'] == model.get(item['field']) %}
                                            <input type="radio" name="{{item['field']}}" value="{{one['value']}}" checked> {{one['name']}}
                                        {% else %}
                                            <input type="radio" name="{{item['field']}}" value="{{one['value']}}"> {{one['name']}}
                                        {% endif %}
                                        &nbsp;&nbsp;&nbsp;&nbsp;
                                    {% endfor %}
                                {% endif %}
                            </div>
                        </div>
                    {% endfor %}
                    <button type="button" class="btn btn-primary mr-2" onclick="save()">保存</button>
                    <a href="{{request.args.get('back')}}" class="btn btn-info">返回</a>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="js/sweetalert2.min.js"></script>
<script src="vendors/js/SwalAide.js"></script>
<script src="js/jquery.min.js"></script>
<script>
    function save() {
        $.post('',$('#form-data').serialize(),
            function (rsp) {
                SwalAide.toast(rsp.message,rsp.error,function(){
                    if(!rsp.error) location = '{{request.args.get('back')}}';
                });
            }, 'json');
    }
</script>
{% endblock %}